#  rest_framework
from rest_framework import exceptions
from rest_framework.authentication import BaseAuthentication
#  models
from user.models import UserToken

class FirstAuthtication(BaseAuthentication):
    #  认证匿名用户

    def authenticate(self, request):
        pass

    def authenticate_header(self, request):
        pass


class Authtication(BaseAuthentication):
    #  认证带有token的请求,间接意味要有登录用户

    def authenticate(self, request):
        token = request.GET.get('token')
        token_obj = UserToken.objects.filter(token=token).first()
        if not token_obj:
            raise exceptions.AuthenticationFailed('用户认证失败')
        #  在rest_framework这两个字段会赋值给request
        return (token_obj.user, token_obj)

    def authenticate_header(self,  request):
        return 'Basic realm="api"'
